//
//  AdView.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/9/10.
//  Copyright (c) 2015年 GripMeMory. All rights reserved.
//

#import "AdView.h"
#import "UIView+Sizes.h"

#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kViewHeight 55.0f

#define YLOCALTION self.bounds.origin.y

static CGFloat const changeImageTime = 2.0f;
static int currentPageNumber = 0;

@interface AdView ()

{
    //timer
    NSTimer *moveTimer;
    //time up flag
    BOOL isTimeUp;
    //
    UIImageView *_leftView;
    UIImageView *_middleView;
    UIImageView *_rightView;
}

@property (nonatomic, retain, readonly) UIImageView *leftView;
@property (nonatomic, retain, readonly) UIImageView *middleView;
@property (nonatomic, retain, readonly) UIImageView *rightView;

@end

@implementation AdView

#pragma mark - Custem UserInterface

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.bounces = NO;
        
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.pagingEnabled = YES;
        self.contentOffset = CGPointMake(0, 0);
        self.contentSize = CGSizeMake(kScreenWidth * 3, self.height);
        self.delegate = self;
        
        _leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kViewHeight)];
        _middleView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kViewHeight)];
        _rightView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * 2, 0, kScreenWidth, kViewHeight)];
        [self addSubview:_leftView];
        [self addSubview:_middleView];
        [self addSubview:_rightView];
        
        moveTimer = [NSTimer scheduledTimerWithTimeInterval:changeImageTime target:self selector:@selector(MoveImage) userInfo:nil repeats:YES];
        isTimeUp = NO;
    }
    return self;
}

- (void)setImageArr:(NSArray *)imageArr{
    _imageArr = imageArr;
    
    _leftView.image = [UIImage imageNamed:_imageArr[4]];
    _middleView.image = [UIImage imageNamed:_imageArr[0]];
    _rightView.image = [UIImage imageNamed:_imageArr[1]];
}

- (void)setPageControlShowStyle:(UIPageControlShowStyle)PageControlShowStyle{
    if (PageControlShowStyle == UIPageControlShowStyleNone) {
        return;
    }
    
    _pageControl = [[UIPageControl alloc] init];
    _pageControl.numberOfPages = _imageArr.count;

    if (PageControlShowStyle == UIPageControlShowStyleLeft) {
        _pageControl.frame = CGRectMake(10, YLOCALTION + kViewHeight - 20, 20 * _pageControl.numberOfPages, 20);
    }else if(PageControlShowStyle == UIPageControlShowStyleCenter){
        _pageControl.frame = CGRectMake(0, 0, 20 * _pageControl.numberOfPages, 20);
        _pageControl.center = CGPointMake(kScreenWidth / 2.0, YLOCALTION + kViewHeight - 10);
    }else{
        _pageControl.frame = CGRectMake(kScreenWidth - 20 * _pageControl.numberOfPages, YLOCALTION + kViewHeight - 20, 20 *_pageControl.numberOfPages, 20);
    }
    
    _pageControl.currentPage = 0;
    _pageControl.enabled = NO;
    [self performSelector:@selector(addPageControl) withObject:nil afterDelay:0.1f];
}

- (void)addPageControl{
    [[self superview] addSubview:_pageControl];
}

#pragma mark - Timer Method
- (void)MoveImage{
    [self setContentOffset:CGPointMake(kScreenWidth * 2, 0) animated:YES];
    isTimeUp = YES;
    [NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(scrollViewDidEndDecelerating:) userInfo:nil repeats:NO];
}

#pragma mark - Decelerate Method
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (self.contentOffset.x == 0) {
        currentPageNumber = (int)((currentPageNumber - 1) + _imageArr.count) % _imageArr.count;
        _pageControl.currentPage = ((_pageControl.currentPage - 1) + _imageArr.count) % _imageArr.count;
    }else if (self.contentOffset.x == kScreenWidth * 2){
        currentPageNumber = (int)((currentPageNumber + 1) + _imageArr.count) % _imageArr.count;
        _pageControl.currentPage = ((_pageControl.currentPage + 1) + _imageArr.count) % _imageArr.count;
    }else{
        return;
    }
    
    _leftView.image = [UIImage imageNamed:_imageArr[((currentPageNumber - 1) + _imageArr.count) % _imageArr.count]];
    _middleView.image = [UIImage imageNamed:_imageArr[((currentPageNumber) + _imageArr.count)% _imageArr.count]];
    _rightView.image = [UIImage imageNamed:_imageArr[((currentPageNumber + 1) + _imageArr.count)% _imageArr.count]];
    
    self.contentOffset = CGPointMake(kScreenWidth, 0);
    
    if (!isTimeUp) {
        [moveTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:changeImageTime]];
    }
    isTimeUp = NO;
    
}

@end
